package com.zcdyy.rentalofhouse.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.zcdyy.rentalofhouse.R;
import com.zcdyy.rentalofhouse.receiver.MyBroadCastReceiver;
import com.zcdyy.rentalofhouse.ui.adapter.PagerAdapter;
import com.zcdyy.rentalofhouse.ui.fragment.MainFragment;
import com.zcdyy.rentalofhouse.ui.fragment.MineFragment;
import com.zcdyy.rentalofhouse.ui.fragment.RentalFragment;
import com.zcdyy.rentalofhouse.utils.BaseTranslucentActivity;
import com.zcdyy.rentalofhouse.views.MyViewPager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zhouchuan on 2017/2/27.
 */

public class MainActivity extends BaseTranslucentActivity implements View.OnClickListener {

    private MyViewPager viewPager;//申明控件 MyViewPager
    private PagerAdapter adapter;//申明适配器 PagerAdapter
    private List<Fragment> fragmentList = new ArrayList<>();//初始化fragmentList
    private ImageView[] bottom = new ImageView[3];//初始化申明底部的3个控件
    //申明底部三个控件的ID
    private int[] tab_ids = {R.id.actionBar_iv_bottom1, R.id.actionBar_iv_bottom2, R.id.actionBar_iv_bottom3};
    //申明底部三个控件的未选中状态
    private int[] tab_unSelect = {R.drawable.footer_index_icon1, R.mipmap.issue_trans, R.drawable.footer_my_icon1};
    //申明底部三个控件的选中状态
    private int[] tab_Select = {R.mipmap.home, R.mipmap.issue, R.mipmap.people};
    //初始化三个控件的状态
    private boolean[] selected = {false, false, false};


    private MyBroadCastReceiver receiver;


    protected void findViewByIDS() {
        viewPager = (MyViewPager) findViewById(R.id.main_viewPager);
        for (int i = 0; i < 3; i++) {
            bottom[i] = (ImageView) findViewById(tab_ids[i]);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewByIDS();
        addFragment();
        bind();
    }

    private void addFragment() {
        //加入首页需要展示的3个fragment
        fragmentList.add(new MainFragment());
        fragmentList.add(new RentalFragment());
        fragmentList.add(new MineFragment());
    }

    private void bind() {
        //底部导航栏三个控件的监听事件
        for (int i = 0; i < 3; i++) {
            bottom[i].setOnClickListener(this);
        }
        //初始化适配器
        adapter = new PagerAdapter(getSupportFragmentManager(), fragmentList);
        //viewpager装载适配器
        viewPager.setAdapter(adapter);

    }

    /**
     * 这个是点击底部导航栏切换页面的方法
     * @param v
     */

    @Override
    public void onClick(View v) {
        for (int i = 0; i < 3; i++) {
            if (v.getId() == tab_ids[i]) {//判断点击的图片ID是不是底部导航栏图片ID中的一个
                if (!selected[i]) {//判断你所点击的图片select状态是不是未选中的状态  false为未选中
                    resetImg();//重置底部导航栏的所有图片
                    bottom[i].setImageResource(tab_Select[i]);//设置选中的背景图片
                    viewPager.setCurrentItem(i);// 切换显示区域
                    selected[i] = true;//将选中的状态设置为true
                    break;
                }

            }
        }
    }

    /**
     * 这个是使底部导航栏的图片恢复成未选中的状态，并将select都设置为false
     */
    public void resetImg() {
        for (int i = 0; i < 3; i++) {
            bottom[i].setImageResource(tab_unSelect[i]);
            selected[i] = false;
        }
    }
}

